package com.beidu.lottery.call.service.portal.filter;

import com.beidu.lottery.call.common.BaseContextHandler;
import com.beidu.lottery.call.constant.ActivityTypeEnum;
import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.entity.dto.LotteryLeaveNumDTO;
import com.beidu.lottery.call.exception.ServiceException;
import com.beidu.lottery.call.service.portal.UserDataService;
import com.beidu.lottery.call.service.portal.chain.AbstractChainHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 抽奖次数校验
 **/
@Component
@Slf4j
@RequiredArgsConstructor
public class LotteryNumValidate implements AbstractChainHandler<PortalActivityRule> {

    private final UserDataService userDataService;

    @Override
    public void handler(PortalActivityRule rule) {
        log.info("LotteryNumValidate start");
        // 不限制次数
        if (rule.getLotteryLimitType() != null && rule.getLotteryLimitType() == 0) {
            return;
        }
        // 查询剩余抽奖次数（用户当日剩余或总数剩余）
        Integer times = userDataService.queryLotteryLeaveNumBloomFilter(LotteryLeaveNumDTO
                        .builder()
                        .activityId(rule.getActivityId())
                        .userId(BaseContextHandler.getUserId())
                        .build());
        if (times < 1) {
            throw new ServiceException("对不起，您的抽奖次数已用完");
        }
    }

    @Override
    public String mark() {
        return ActivityTypeEnum.LOTTERY.getType();
    }

    @Override
    public int getOrder() {
        return 30;
    }
}